//
//  LWLoopView.m
//  轮播器封装
//
//  Created by Alphts on 16/7/10.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "LWLoopView.h"

@interface LWLoopView ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSTimer *timer;

//接受外部给与的字符串数组
@property (nonatomic, strong) NSArray *photoURLStr;


@end

@implementation LWLoopView

@synthesize photoURLStr = _photoURLStr;

- (instancetype)initWithItemSize:(CGSize)itemSize loopStyle:(kLoopStyle)style{
    
    if (self = [super initWithFrame:CGRectZero collectionViewLayout:self.flowLayout]) {
        
        self.flowLayout.itemSize = itemSize;
        
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        
        self.dataSource = self;
        self.delegate = self;
        
        self.timeInterval = 3.0f;
        self.autoLoop = NO;
        self.style = style;
        
        [self registerClass:[LWLoopCell class] forCellWithReuseIdentifier:@"loopCell"];
        
    }
    return self;
    
}

- (instancetype)initWithItemSize:(CGSize)itemSize{
    
    return [self initWithItemSize:itemSize loopStyle:kLoopStylePageAndTitle];
}

- (void)setupTimer{
    
    self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(photoRun) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)photoRun{
    
    [self setContentOffset:CGPointMake(self.contentOffset.x+self.frame.size.width, 0) animated:YES];
    
}


#pragma mark - 自定义方法

- (NSArray *)photoURLStr{
    
    return [self.loopDelegate photoURLStrWithLoopView:self];
}


#pragma mark - 重写setter方法

- (void)setAutoLoop:(BOOL)autoLoop{
    _autoLoop = autoLoop;
    if (autoLoop) {
        [self setupTimer];
    }
}

#pragma mark - 数据源和代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoURLStr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LWLoopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"loopCell" forIndexPath:indexPath];
    
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1.0];
    
    cell.style = self.style;
    
    
    [cell.iv_photo sd_setImageWithURL:self.photoURLStr[indexPath.item]];

    if (self.style != kLoopStyleNone){
        
        cell.totalCount = self.photoURLStr.count;
        
        cell.currentCount = indexPath.item ;
        
        cell.currentPageColor = self.currentPageColor;
        
        cell.pageColor = self.pageColor;
        
        if (self.style == kLoopStylePageAndTitle){
            
            cell.title = self.titleArray[indexPath.item];
            cell.titleFont = self.titleFont;
            cell.titleColor = self.titleColor;
            
        }
    }
    
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger item = self.contentOffset.x / self.frame.size.width;
    
    item = item % self.photoURLStr.count;
    
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:1] atScrollPosition:0 animated:NO];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (self.autoLoop) {
        [self setupTimer];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    if (!self.autoLoop) {
        return;
    }
    
    NSInteger item = self.contentOffset.x / self.frame.size.width;
    
    item = item % self.photoURLStr.count;
    
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:1] atScrollPosition:0 animated:NO];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (self.autoLoop) {
        [self.timer invalidate];
    }
}


#pragma mark - lazyload
- (UICollectionViewFlowLayout *)flowLayout{
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置flowLayout的属性
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _flowLayout;
}

- (void)dealloc{
    if (self.autoLoop) {
        [self.timer invalidate];
    }
}


@end







